package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.entity.ResponseEntity;
import com.example.entity.Assessor;
import com.example.entity.Result;
import com.example.entity.StudentInformation;
import com.example.service.AssessorPlusService;
import com.example.service.AssessorResultService;
import com.example.service.AssessorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评审对象控制类
 * @author 李平
 * @Date 2022-5-2
 */
@RestController
public class AssessorController {

    @Autowired
    private AssessorService assessorService;

    @Autowired
    private AssessorPlusService assessorPlusService;

    @Autowired
    private AssessorResultService assessorResultService;

    private static final Logger LOG = LoggerFactory.getLogger(AssessorController.class);

    /**
     * 评审人员角色登录接口
     * @param assessor
     * @return ResponseEntity
     */
    @PostMapping
    @ResponseBody
    @RequestMapping("/assessor-login")
    public ResponseEntity login(@RequestBody Assessor assessor){
        try {
            if (StringUtils.isEmpty(assessor.getPhone())){
                LOG.error("电话号码不能为空");
                return ResponseEntity.err("电话号码不能为空");
            }
            if (StringUtils.isEmpty(assessor.getPassword())){
                LOG.error("密码不能为空");
                return ResponseEntity.err("密码不能为空");
            }
            // 登录校验业务
            Assessor stu = assessorService.getAssessorByPAndP(assessor.getPhone(), assessor.getPassword());
            if (stu != null){
                LOG.info("用户登录成功：" + assessor.getPhone());
                return  ResponseEntity.ok(stu);
            }else{
                LOG.error(assessor.getPhone() + "用户登录失败");
                return ResponseEntity.err("用户登录失败");
            }
        }catch(Exception e){
            e.printStackTrace();
            LOG.error("用户登录失败：" + assessor.getPhone());
            return ResponseEntity.err(e.getMessage());
        }
    }

    /**
     * 评审人员对象注册接口
     * @param assessor
     * @return ResponseEntity
     */
    @PostMapping
    @ResponseBody
    @RequestMapping("/assessor-register")
    public ResponseEntity register(@RequestBody Assessor assessor){
        try {
            if (assessor == null){
                LOG.error("评审人员对象不能为空");
                return ResponseEntity.err("评审人员对象不能为空");
            }
            // 注册业务
            Boolean b = assessorService.addAssessor(assessor);
            if (b) {
                // 当前电话号码未被注册，注册成功
                LOG.info(assessor.getPhone() + "注册成功");
                return ResponseEntity.ok(assessorService.getAssessorByPhone(assessor.getPhone()));
            }else {
                // 当前电话号码已被注册，无法注册
                LOG.error(assessor.getPhone() + "已被注册");
                return ResponseEntity.err(assessor.getPhone() + "已被注册");
            }
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error("用户注册失败：" + assessor.getPhone());
            return ResponseEntity.err(e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param info
     * @return
     */
    @PostMapping
    @ResponseBody
    @RequestMapping(value = "/assessor-getStudentInfo/{currentPage}/{pageSize}")
    public ResponseEntity getStudentInfo(@PathVariable Integer currentPage,@PathVariable Integer pageSize,
                                         @RequestBody StudentInformation info) {
//        List<Student> data = adminService.getAllStudent(info);
        IPage<StudentInformation> page = assessorPlusService.getPage(currentPage, pageSize, info);
        if (currentPage > page.getPages()) {
            page = assessorPlusService.getPage((int) page.getPages(), pageSize, info);
        }
        return ResponseEntity.ok(page);
    }

    @PostMapping
    @ResponseBody
    @RequestMapping("/assessor-getOneResult/{id}")
    public ResponseEntity getResult(@PathVariable String id){
        Result r = assessorResultService.getById(id);
        return ResponseEntity.ok(r);
    }

    @PostMapping
    @ResponseBody
    @RequestMapping("/assessor-setFirstResult/{id}/{is}")
    public ResponseEntity setFirstResult(@PathVariable String id,@PathVariable Integer is){
        Boolean b = assessorResultService.setResult(id, is);
        if(b){
            return ResponseEntity.ok("审核成功");
        }else{
            return ResponseEntity.err("操作失败");
        }
    }

    @GetMapping
    @ResponseBody
    @RequestMapping("/getVoteName")
    public ResponseEntity getVoteName(){
        List<StudentInformation> voteName = assessorPlusService.getVoteName();
        if (voteName!=null&&voteName.size()>0){
            return ResponseEntity.ok(voteName);
        }else{
            return ResponseEntity.err("无可参加投票人员");
        }
    }


}
